package com.lottery.controller;

import com.lottery.dao.UpPlanDao;
import com.lottery.entity.Lottery;
import com.lottery.entity.Query;
import com.lottery.entity.UpPlan;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by yeyang on 2017/3/15.
 */
@Service
public class Mainserver {
    @Autowired
    UpPlanDao upPlanDao;

    public List<Lottery> getLotteryList (Query query){
        List<UpPlan> upPlens = upPlanDao.getAllList(query);
        Map<String, Lottery> lotteries = new HashMap<>();
        Map<String, Integer> map1 = new HashMap<>();
        List<Lottery> results = new ArrayList<>();
        for (UpPlan upPlan : upPlens) {
            if (null == upPlan) {
                continue;
            }
            Lottery lottery = lotteries.get(upPlan.getPeriods() + "," + upPlan.getProvince());
            if (lottery == null) {
                lottery = new Lottery();
            }
            String periods = upPlan.getPeriods();
            if (StringUtils.isNotBlank(periods)) {

                String no = periods.substring(periods.length() - 2, periods.length());

                lottery.setNo(no);
            }

            setResult(upPlan, map1);
            if ("1".equals(upPlan.getScheme())) {
                lottery.setInvest(upPlan.getInvest());
                lottery.setBonus(upPlan.getBonus());
                lottery.setPeriods(upPlan.getPeriods());
                lottery.setProvince(upPlan.getProvince());
                lottery.setPlay(upPlan.getPlay());
                lottery.setMultiple(upPlan.getMultiple());
                lottery.setNumber1(upPlan.getNumber1());
                lottery.setNumber2(upPlan.getNumber2());
                lottery.setNumber3(upPlan.getNumber3());
                lottery.setOperator(upPlan.getOperator());
                lottery.setTime(upPlan.getTime());
                lottery.setShoot(upPlan.getShoot());
                lottery.setScheme(upPlan.getScheme());
                lottery.setResults(upPlan.getResults());
                lotteries.put(upPlan.getPeriods() + "," + upPlan.getProvince(), lottery);
            } else if ("2".equals(upPlan.getScheme())) {
                lottery.setPeriods(upPlan.getPeriods());
                lottery.setInvestTwo(upPlan.getInvest());
                lottery.setBonusTwo(upPlan.getBonus());
                lottery.setResults(upPlan.getResults());
                lottery.setProvince(upPlan.getProvince());
                lottery.setPlayTwo(upPlan.getPlay());
                lottery.setShootTwo(upPlan.getShoot());
                lottery.setMultipleTwo(upPlan.getMultiple());
                lottery.setNumber1Two(upPlan.getNumber1());
                lottery.setNumber2Two(upPlan.getNumber2());
                lottery.setNumber3Two(upPlan.getNumber3());
                lottery.setOperatorTwo(upPlan.getOperator());
                lottery.setTimeTwo(upPlan.getTime());
                lottery.setSchemeTwo(upPlan.getScheme());
                lotteries.put(upPlan.getPeriods() + "," + upPlan.getProvince(), lottery);
            } else {
                lottery.setInvestThree(upPlan.getInvest());
                lottery.setBonusThree(upPlan.getBonus());
                lottery.setPeriods(upPlan.getPeriods());
                lottery.setResults(upPlan.getResults());
                lottery.setProvince(upPlan.getProvince());
                lottery.setPlayThree(upPlan.getPlay());
                lottery.setShootThree(upPlan.getShoot());
                lottery.setMultipleThree(upPlan.getMultiple());
                lottery.setNumber1Three(upPlan.getNumber1());
                lottery.setNumber2Three(upPlan.getNumber2());
                lottery.setNumber3Three(upPlan.getNumber3());
                lottery.setOperatorThree(upPlan.getOperator());
                lottery.setTimeThree(upPlan.getTime());
                lottery.setSchemeThree(upPlan.getScheme());
                lotteries.put(upPlan.getPeriods() + "," + upPlan.getProvince(), lottery);
            }

        }
        Iterator iter = lotteries.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            results.add((Lottery) entry.getValue());
        }

        return results;
    }

    public void setResult(UpPlan upPlan, Map<String, Integer> map) {

        try {
        	BigDecimal doubleInvest = new BigDecimal(upPlan.getInvest()*upPlan.getMultiple());  
            upPlan.setInvest(doubleInvest.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            BigDecimal doubleBonus = new BigDecimal(upPlan.getBonus()*upPlan.getMultiple()); 
            upPlan.setBonus(doubleBonus.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            BigDecimal doublemul = new BigDecimal(upPlan.getMultiple());
            upPlan.setMultiple(doublemul.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue());
        } catch (Exception e) {
        }

        String results = upPlan.getResults();
        String[] res= new String[0];
       if(null!=results){
          res = results.split(",");
       }
        int count = 0;
        int number1 = 10000000;
        int number2 = 10000000;
        int number3 = 10000000;
        try {
            number1 = Integer.parseInt(upPlan.getNumber1().trim());

        } catch (Exception e) {

        }
        try {
            number2 = Integer.parseInt(upPlan.getNumber2().trim());

        } catch (Exception e) {
        }
        try {
            number3 = Integer.parseInt(upPlan.getNumber3().trim());
        } catch (Exception e) {

        }

        for (String str : res) {
            int jiang = 0;
            try {
                jiang = Integer.parseInt(str.trim());

            } catch (Exception e) {

            }
            if (jiang == number1 || jiang == number2 || jiang == number3) {
                count++;
            }
        }

        if ("任2".equals(upPlan.getPlay())) {
            if (count >= 2) {
                upPlan.setShoot("\t&radic;");
            }

        } else if ("任3".equals(upPlan.getPlay())) {

            if (count >= 3) {
                upPlan.setShoot("\t&radic;");
            }
        } else if ("任5".equals(upPlan.getPlay())) {
            if (count < 1) {
                upPlan.setShoot("\t&radic;");
            }
        }
        String key = upPlan.getProvince() + upPlan.getScheme() + upPlan.getPlay();

        Integer y = map.get(key);
        int x = 0;
        if (null != y) {
            x = y;
        }

        if (!"\t&radic;".equals(upPlan.getShoot())) {
            if (0 != x) {
                x++;
                upPlan.setShoot(String.valueOf(x));
                map.put(key, x);
            } else {
                upPlan.setShoot("1");
                map.put(key, 1);
            }
        } else {
            map.put(key, 0);

        }

    }
}
